﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;

namespace DATN.mds.ActionPlanManagement
{
    public partial class EditAP : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Load_data();
                //TextBox1.Enabled = false;
                TextBox2.Enabled = false;
                TextBox3.Enabled = false;
                TextBox4.Enabled = false;
                TextBox5.Enabled = false;
                TextBox6.Enabled = false;
                //TextBox7.Enabled = false;
                bt_save.Enabled = false;

            }
        }

        protected void Load_data()
        {
            SqlConnection cnn = new SqlConnection();
            cnn.ConnectionString = ConfigurationManager.ConnectionStrings["DATNconnection"].ToString();
            cnn.Open();
            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter("Select * From ActionPlan Where Ac_Id = " + Convert.ToInt32(Session["Ac_Id"].ToString()), cnn);

            da.Fill(ds, "ActionPlan");

            //TextBox1.Text = ds.Tables["ActionPlan"].Rows[0].ItemArray[0].ToString();
            TextBox2.Text = ds.Tables["ActionPlan"].Rows[0].ItemArray[1].ToString();
            TextBox7.Text = ds.Tables["ActionPlan"].Rows[0].ItemArray[2].ToString();
            string text = ds.Tables["ActionPlan"].Rows[0].ItemArray[3].ToString();
            if (text == "")
            {
                TextBox3.Text = "";
            }
            else
            {
                DateTime text1 = Convert.ToDateTime(text);
                TextBox3.Text = text1.ToShortDateString();
            }
            TextBox4.Text = ds.Tables["ActionPlan"].Rows[0].ItemArray[4].ToString();
            

            decimal txt5 =decimal.Parse(ds.Tables["ActionPlan"].Rows[0].ItemArray[5].ToString());
            decimal txt6 = decimal.Parse(ds.Tables["ActionPlan"].Rows[0].ItemArray[6].ToString());
            
            TextBox5.Text = string.Format("{0:0,0}", txt5);
            TextBox6.Text = string.Format("{0:0,0}", txt6);
            //TextBox7.Text = ds.Tables["ActionPlan"].Rows[0].ItemArray[7].ToString();


            ds.Dispose();
            da.Dispose();
            cnn.Close();
        }
        protected void bt_edit_Click(object sender, EventArgs e)
        {
            TextBox2.Enabled = true;
            TextBox3.Enabled = true;
            TextBox4.Enabled = true;
            TextBox5.Enabled = true;
            TextBox6.Enabled = true;
            //TextBox7.Enabled = true;
            bt_save.Enabled = true;
        }

        protected void bt_save_Click(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                string name = TextBox2.Text;
                string time = TextBox3.Text;
                string location = TextBox4.Text;
                double actual_ex = double.Parse(TextBox5.Text);
                double actual_re = double.Parse(TextBox6.Text);
                string des = TextBox7.Text;
                //string Opp_Id = TextBox7.Text;
                DateTime time1 = new DateTime();


                try
                {
                    time1 = Convert.ToDateTime(time);
                }
                catch (Exception ex)
                {
                    
                }
                time = time1.ToString("MM/dd/yyyy hh:mm:ss"); 

                SqlConnection cnn = new SqlConnection();
                cnn.ConnectionString = ConfigurationManager.ConnectionStrings["DATNconnection"].ToString();

                string com1 = "Update ActionPlan "
                    + "\n Set Name = N'" + name + "',"
                    + "Time = '" + time + "',"
                    + "Location = N'" + location + "',"
                    + "ActualExpense = '" + actual_ex + "',"
                    + "ActualRevenue = '" + actual_re + "',"
                    + "Description = N'" + des + "'"
                    + "\n Where Ac_Id =" + Convert.ToInt32(Session["Ac_Id"].ToString());

                SqlCommand cmd = cnn.CreateCommand();
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = com1;

                cnn.Open();
                cmd.ExecuteNonQuery();
                cnn.Close();

                Page.Response.Redirect("~/mds.ActionPlanManagement/IndexAP.aspx");
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            SqlConnection cnn = new SqlConnection();
            cnn.ConnectionString = ConfigurationManager.ConnectionStrings["DATNconnection"].ToString();

            string com1 = "Delete From ActionPlan Where Ac_Id =" + Convert.ToInt32(Session["Ac_Id"].ToString());

            SqlCommand cmd = cnn.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = com1;

            cnn.Open();
            cmd.ExecuteNonQuery();
            cnn.Close();

            Page.Response.Redirect("~/mds.ActionPlanManagement/IndexAP.aspx");
        }
    }
}